#include "addnew.h"
#include "ui_addnew.h"
#include "common.h"
#include "mypostman.h"

AddNew::AddNew(MyPostman *myPostMan, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AddNew)
{
    ui->setupUi(this);
    setFixedSize(400,300);
    _myPostMan = myPostMan;
}

AddNew::~AddNew()
{
    delete ui;
}

void AddNew::on_btn_confirm_clicked()
{

    QSqlQuery sql_query;
    QString User = ui->lineEdit_user->text();
    QDateTime now = QDateTime::currentDateTime();
    QString nowTime = now.toString("yyyyMMddhhmmss");
    QString createUser =QString("insert into User(UserName,PassWord,UpdateTime) values('%1','','%2');").arg(User).arg(nowTime);
    qDebug()<<"创建用户的sql为"<<createUser;
    if(!sql_query.exec(createUser))
    {
        qDebug() << sql_query.lastError();
        this->close();
    }
    else
    {
        qDebug() << "created a new user!";
        _myPostMan->getUserList();
        this->close();
    }
}

void AddNew::on_btn_return_clicked()
{
    this->close();
}
